From: Florian Müllner Date: Mon, 10 Jul 2023 04:19:53 +0000 (+0200) Subject: extensionBase: Add static lookupByURL() method X-Git-Tag: archive/raspbian/50.5-1+rpi1~1^2~337^2~1^2~96 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=cd99fbae50b7d08a1ce93440d7a93f57cb098155;p=gnome-shell.git extensionBase: Add static lookupByURL() method With convenience API like getSettings() now being provided by the ExtensionObject subclass, extensions will need to access their entry point more often. Having to pass a pointer through the hierarchy can be annoying, so add a static method that allows them to look it up: ```js const ext = Extension.lookupByURL(import.meta.url); this._settings = ext.getSettings(); ``` Part-of: --- diff --git a/js/extensions/sharedInternals.js b/js/extensions/sharedInternals.js index 89906d9da..8ec152332 100644 --- a/js/extensions/sharedInternals.js +++ b/js/extensions/sharedInternals.js @@ -20,6 +20,32 @@ export function setExtensionManager(extensionManager) { export class ExtensionBase { #gettextDomain; + /** + * Look up an extension by URL (usually 'import.meta.url') + * + * @param {string} url - a file:// URL + */ + static lookupByURL(url) { + if (!url.startsWith('file://')) + return null; + + // Keep the last '/' from 'file://' to force an absolute path + let path = url.slice(6); + + // Walk up the directory tree, looking for an extension with + // the same UUID as a directory name. + do { + path = GLib.path_get_dirname(path); + + const dirName = GLib.path_get_basename(path); + const extension = _extensionManager.lookup(dirName); + if (extension !== undefined) + return extension.stateObj; + } while (path !== '/'); + + return null; + } + /** * @param {object} metadata - metadata passed in when loading the extension */